package com.foreveross.project.projectonline.web.controller;

import java.util.List;

import com.foreveross.project.projectonline.config.NeedLogin;
import com.foreveross.project.projectonline.model.TpMgtUser;
import com.foreveross.project.projectonline.service.MgtUserService;
import com.jfinal.aop.Before;
import com.jfinal.plugin.spring.Inject;
import com.jfinal.plugin.spring.IocInterceptor;

/**
 * 
 * @author Administrator
 *
 */
@Before(IocInterceptor.class)
@NeedLogin
public class UserAdminController extends BaseController {
	
	@Inject.BY_NAME
	MgtUserService mgtUserService;
	
	public void index() {
		render("/pages/userAdmin.html");
	}
	
	public void userList(){
		List list  = mgtUserService.getUserList(getPara("LOGIN_EMAIL"));
		setAttr("userList", list);
		setAttr("LOGIN_EMAIL", getPara("LOGIN_EMAIL"));
		index();
	}
	
	public void toAddUser(){
		render("/pages/addUser.html");
	}
	
	public void addUser(){
		boolean b = mgtUserService.addUser(getPara("name"), getPara("password"), getPara("email"), getPara("desc"), getPara("state"));
		{
			setAttr("name", getPara("name"));
			setAttr("password", getPara("password"));
			setAttr("email", getPara("email"));
			setAttr("desc", getPara("desc"));
		}
		if(b) {
			setAttr("b", true);
			setAttr("info", "增加用户成功！");
			
		} else {
			setAttr("b", false);
			setAttr("info", "增加用户失败！登陆EMAIL已经存在。");
		}
		render("/pages/addUser.html");
	}
	
	
	public void toEditUser(){
		TpMgtUser user = mgtUserService.getUser(getPara("id"));
		setAttr("user", user);
		render("/pages/editUser.html");
	}
	
	public void editUser(){
		int b = mgtUserService.editUser(getPara("id"), getPara("oldEmail"), getPara("name"), getPara("password"), getPara("email"), getPara("desc"), getPara("state"));
		{
			TpMgtUser user = mgtUserService.getUser(getPara("id"));
			setAttr("user", user);
		}
		if(b == 0){
			setAttr("b", false);
			setAttr("info", "用户信息编辑失败！你要修改的登陆EMAIL已经存在。");
		} else if(b == 2) {
			setAttr("b", false);
			setAttr("info", "系统发生异常，请联系管理员。");
		} else if(b == 1){
			setAttr("b", true);
			setAttr("info", "编辑用户信息成功！");
		}
		render("/pages/editUser.html");
	}
	
	public void deleteUser(){
		TpMgtUser user = mgtUserService.getUser(getPara("id"));
		if(user.get("CATEGORY").equals("SYS")) {
			setAttr("b", false);
			setAttr("info", "该账号为系统管理员不允许删除！请联系管理员。");
			userList();
			return;
		} else {
			//查找是否有项目挂在该账号下，如果有则，返回失败
			
		}

		boolean b = mgtUserService.deleteUser(getPara("id"));
		if(b) {
			setAttr("b", true);
			setAttr("info", "删除用户成功！");
			
		} else {
			setAttr("b", false);
			setAttr("info", "系统发生异常，删除用户失败！请联系管理员。");
		}
		
		userList();
		
	}
	
}
